package com.wanxi.copy;

import com.wanxi.util.Tool;

import java.io.*;

public class MyCopyFile {
    public static void main(String[] args) {
        String oldPath = "E:\\wanxi\\step-2\\java02-0921\\day20IO\\src\\com\\wanxi\\io\\temp\\华锐_01.jpg";
        String newPath = "E:\\wanxi\\step-2\\java02-0921\\day20IO\\src\\com\\wanxi\\copy";
        String fileName = "华锐_01.jpg";
        if (new File(oldPath).exists()) {
            File file = Tool.mkFile(newPath, fileName);
            copy(new File(oldPath), file);
        } else {
            System.out.println("文件不存在，不能进行复制");
        }
    }

    private static void copy(File oldFile, File newFile) {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            inputStream = new FileInputStream(oldFile);
            outputStream = new FileOutputStream(newFile);
//            int read = 0;
//            while ((read = inputStream.read()) != -1) {
//                outputStream.write(read);
//            }
            byte[] bytes = new byte[100];
            int read = 0;
            while ((read = inputStream.read(bytes)) != -1) {
//              bytes每次读取的数据
//                0 表示每次从数组里面的第一个开始获取数据
//                read  可以理解为每次读取bytes数组里面的个数
                outputStream.write(bytes, 0, read);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                ;
            }
        }

    }
}
